package com.jsz.peini.server.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsz.peini.bean.coupon.CouponInfo;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.common.util.SpringContextUtil;
import com.jsz.peini.mapper.coupon.CouponInfoMapper;

public class CouponUseThread extends Thread {
	private static final Logger logger = LoggerFactory.getLogger(CouponUseThread.class);
	private CouponInfoMapper couponInfoMapper;
	
	public void init(){
		this.start();
	}
	
	@Override
	public void run() {
		super.run();
		
		couponInfoMapper = (CouponInfoMapper)SpringContextUtil.getBean(CouponInfoMapper.class);
		
		while(true)
		{
			try {
				CouponInfo couponInfo = PeiniUtils.useCoupons.take();
				
				CouponInfo info = couponInfoMapper.selectByPrimaryKey(couponInfo.getId());
				Long useNum = info.getUsenum()==null ? 0 : info.getUsenum();
				useNum += couponInfo.getUsenum();
				info.setUsenum(useNum);
				couponInfoMapper.updateByPrimaryKey(info);
				
				Thread.sleep(1);
				/*	}
				}*/
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
			}
		}
		
	}
}
